<!DOCTYPE html>
<html  xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>智慧之窗乡村管理系统</title>
    <link rel="stylesheet" th:href="@{/static/layui/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/static/admin/style.css}" media="all">
    <link rel="stylesheet" th:href="@{/layui/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/admin/style.css}" media="all">
    <link rel="stylesheet" th:href="@{/layui/css/element-index.css}" media="all">
    <script type="text/html" id="indexTpl">
        {{d.LAY_TABLE_INDEX+1}}
    </script>
</head>
<body class="layui-layout-body">
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">农民信息列表</div>
                <div class="layui-card-body">
                    <table class="layui-hide" id="main-table" lay-filter="main-table"></table>
                    <script type="text/html" id="toolbarDemo">
                        <span class="layui-inline" lay-event="add"><i class="layui-icon">&#xe654;</i></span>
                        <span class="layui-inline" style="width: 240px;">
                                   <input class="layui-input" name="param" id="param" placeholder="根据姓名查询" autocomplete="off">
                               </span>
                        <span class="layui-btn layui-btn-sm layui-bg-red" lay-event="querySearch">
                                   <i class="layui-icon">&#xe615;</i>查询
                               </span>
                    </script>
                    <script type="text/html" id="barDemo">
                        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>

<script th:src="@{/static/layui/layui.js}"></script>
<script  th:src="@{/layui/layui.js}"></script>
<script src="/layui/lay/modules/jquery.js" th:src="@{/layui/lay/modules/jquery.js}"></script>
<script src="/layui/lay/modules/table.js" th:src="@{/layui/lay/modules/table.js}"></script>
<script src="/layui/lay/modules/element-index.js" th:src="@{/layui/lay/modules/element-index.js}"></script>
<script>
    layui.use(['table','form'],function () {
        var $ = layui.$;
        var table = layui.table;
        table.render({
            elem:'#main-table',
            height:650,
            url:'/affairs/affairsQueryPage',
            method:'POST',
            page:true,
            id:'table-load',
            toolbar:'#toolbarDemo',
            cols:[
                [
                    {title:'id',templet:'#indexTpl',align:'center'},
                    {field:'type',title:'事务类型',align:'center'},
                    {field:'titles',title:'标题',align:'center'},
                    {field:'image',title:'图片',align:'center',
                        templet: function(d) {
                            return "<div><img src='" + d.image + "' style='width:50px;height:50px;'/></div>";
                        }
                    },
                    {field:'description',title:'描述',align:'center'},
                    {field:'startdate',title:'开始时间',align:'center',
                        templet: "<div>{{layui.util.toDateString(d.startdate,'yyyy-MM-dd')}}</div>"},
                    {field:'enddate',title:'结束时间',align:'center',
                        templet: "<div>{{layui.util.toDateString(d.enddate,'yyyy-MM-dd')}}</div>"},
                    {field:'sates',title:'状态',align:'center'},
                    {field:'person',title:'事务负责人',align:'center'},
                    {field:'remarks',title:'备注信息',align:'center'},
                    {title:'操作',align:'center',toolbar:'#barDemo',fixed:'right'}

                ]
            ]
        });
        //监听头部工具栏事件
        table.on('toolbar(main-table)',function (obj) {
            switch (obj.event) {
                case 'add':
                    layer.open({
                        type:2,
                        title:'新增药品信息',
                        content:'/affairs/affairsPage',
                        shade:[0.8,'#393d49'],//弹框的透明度
                        area:['600px','530px'],//区域 宽高的设置
                        btn:['确定','取消'],
                        yes:function (index,layero) {
                            var iframWindow = window['layui-layer-iframe'+index];
                            var submit = layero.find('iframe').contents().find("#LAY-front-submit");

                            //监听提交
                            iframWindow.layui.form.on('submit(LAY-front-submit)',function (data) {
                                var field = data.field;
                                $.ajax({
                                    url:'/affairs/addaffairs',
                                    data:field,
                                    adync:false,//异步的
                                    cache:false,//要不要缓存
                                    success:function (str) {

                                        if (str.code === 0){
                                            table.reload('table-load');
                                        }
                                        layer.msg(str.msg,{icon:str.icon,anim:str.anim});
                                    }
                                });
                                layer.close(index);//关闭弹窗
                            });
                            submit.trigger('click');
                        },
                        success:function (layero,index) {

                        }

                    })
                    break;
                case 'querySearch':
                    var param = $('#param').val();
                    table.reload('table-load',{
                        where:{
                            param:param
                        }
                    });
                    $('#param').valueOf(param);
                    break;
            }
        })
        //监听编辑和删除
        table.on('tool(main-table)',function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'edit':
                    layer.open({
                        type:2,
                        title:'编辑农民信息',
                        content:'/affairs/affairsQueryById?id='+data.id,
                        shade:[0.8,'#393d49'],//弹框的透明度
                        area:['600px','500px'],//区域 宽高的设置
                        btn:['确定','取消'],
                        yes:function (index,layero) {
                            var iframWindow = window['layui-layer-iframe'+index];
                            var submit = layero.find('iframe').contents().find("#LAY-front-submit");

                            //监听提交
                            iframWindow.layui.form.on('submit(LAY-front-submit)',function (data) {
                                var field = data.field;
                                $.ajax({
                                    url:'/affairs/updateaffairs',
                                    data:field,
                                    adync:false,//异步的
                                    cache:false,//要不要缓存
                                    success:function (str) {

                                        if (str.code === 0){
                                            table.reload('table-load');
                                        }
                                        layer.msg(str.msg,{icon:str.icon,anim:str.anim});
                                    }
                                });
                                layer.close(index);//关闭弹窗
                            });
                            submit.trigger('click');
                        },
                        success:function (layero,index) {

                        }

                    })
                    break;
                case 'del':
                    layer.confirm("确认要删除吗？",function (index) {
                        $.ajax({
                            url:'/affairs/deleteaffairs?id='+data.id,
                            data:null,
                            adync:false,//异步的
                            cache:false,//要不要缓存
                            success:function (str) {

                                table.reload('table-load');//刷新表格

                                layer.msg(str.msg,{icon:str.icon,anim:str.anim});
                            }
                        });
                    });
                    break;
            }
        });

    });
</script>
</body>
</html>